package com.example.smartall.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.smartall.R;
import com.example.smartall.api.Api;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.api.NetCallBack;
import com.example.smartall.diaglog.LoadedDialog;
import com.example.smartall.entity.ActDetailsBean;
import com.example.smartall.ui.base.BaseActivity;
import com.example.smartall.utils.Constant;
import com.example.smartall.utils.SPUtils;

import java.io.IOException;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

 /**
  * @Author:         abbas
  * @CreateDate:     2022/2/8
  * @Description:    文档不清不楚
  *                  数据量传递好像有点大，模拟器会卡
  *                  活动页面 一个webView 用于加载数据
  *                  一个参加活动的按钮
  */
public class ActContentActivity extends BaseActivity {

    private ActDetailsBean.DataBean detailsBean;

    private ImageView imBack;
    private TextView tvTitle;
    private WebView webView;
    private Button btIn;

    private boolean isSign = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_content);

        detailsBean = (ActDetailsBean.DataBean) getIntent().getSerializableExtra("data");
        if (detailsBean == null){
            showToast("数据错误,检查程序");
        }

        init();
        initEvent();
    }

     @Override
     protected void onResume() {
         super.onResume();
         isSign();
     }

     @Override
    public void init() {
        imBack = (ImageView) findViewById(R.id.im_back);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        webView = (WebView) findViewById(R.id.webView);
        btIn = (Button) findViewById(R.id.bt_in);

        tvTitle.setText(detailsBean.getName());
        webView.loadDataWithBaseURL(null,detailsBean.getContent(),null,"utf-8",null);
        webView.getSettings().setJavaScriptEnabled(true);
    }

    @Override
    public void initEvent() {
        imBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActContentActivity.this.finish();
            }
        });
        btIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isSign){
                    //如果网络不好的话没有加载出来，也是参与过了
                    showToast("感谢参与此次活动~");
                }else {
                    sign();
                }
            }
        });
    }

     /**
      * @Author:         abbas
      * @CreateDate:     2022/2/8
      * @Description:    android.os.NetworkOnMainThreadException 4.0后不再允许将网络请求放在主线程当中
      *                  一开始想点击参加的时候再去查有没有参加过，结果网络访问不允许堵塞线程。创建线程的话就和异步没什么区别了
      *                  如果再实际项目当中应该在显示活动数据之前获得用户是否参加了活动，否则不予显示数据
      */
    private void isSign() {
        LoadedDialog dialog = new LoadedDialog(ActContentActivity.this);
        dialog.show();

        Api.config(ApiConfig.ACT_IS_SIGN,null).getRequest(ActContentActivity.this, new NetCallBack() {
            @Override
            public void onSuccess(String res) {
                if (res.contains("false")) isSign = false;
                dialog.dismiss();
            }

            @Override
            public void onFailure(Exception e) {
                dialog.dismiss();
            }
        },"?activityId=" + detailsBean.getId());
    }

    private void sign(){
        HashMap<String,Object> params = new HashMap<>();
        params.put("activityId",detailsBean.getId());
        Api.config(ApiConfig.ACT_SIGN,params).postRequest(new NetCallBack() {
            @Override
            public void onSuccess(String res) {
                if (res.contains("200")){
                    isSign = true;
                    showToastSync("参与活动成功");
                }else {
                    showToastSync(res);
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        },SPUtils.config(ActContentActivity.this, Constant.SP_USER_FILE_NAME).getString(Constant.TOKEN));
    }
}